package com.socialbank.web.util;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.http.Consts;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.ParseException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

/**
 * 
 * @Package: com.socialbank.web.util
 * @author: kangxiaohui
 * @date: 2017年8月29日 上午11:07:19 此系统调动别的系统的url ,系统内进行url的处理 java 调用
 */
public class HttpUtils {

	/**
	 * get 请求 带参数
	 * 
	 * @param map
	 * @param url
	 * @return
	 */
	public static String get(Map<String, Object> map, String url) {
		CloseableHttpClient httpclient = HttpClients.createDefault();
		String result = null;
		String str = null;
		List<NameValuePair> params = parseMap(map);
		// 转换为键值对
		try {
			str = EntityUtils.toString(new UrlEncodedFormEntity(params, Consts.UTF_8));
			// 创建httpget.
			HttpGet httpget = new HttpGet(url + "?" + str);
			//httpget.setHeader(ContentType.DEFAULT_TEXT.getMimeType(),Consts.UTF_8.toString());
			System.out.println("executing request " + httpget.getURI());
			System.out.println("executing content " + str);
			// 执行get请求.
			CloseableHttpResponse response = httpclient.execute(httpget);
			try {
				// 获取响应实体
				HttpEntity entity = response.getEntity();
				System.out.println("--------------------------------------");
				// 打印响应状态
				System.out.println(response.getStatusLine());
				if (entity != null) {
					result = EntityUtils.toString(entity);
					// 打印响应内容长度
					System.out.println("Response content length: " + entity.getContentLength());
					// 打印响应内容
					System.out.println("Response content: " + result);
				}
				System.out.println("------------------------------------");
			} finally {
				response.close();
			}
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (ParseException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			// 关闭连接,释放资源
			try {
				httpclient.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return result;
	}

	/**
	 * 请求方式为json的post请求
	 * 
	 * @param map
	 * @param url
	 * @return
	 */
	public static String postWithJSON(Map<String, Object> map, String url) {
		String result = null;
		// 创建默认的httpClient实例.
		CloseableHttpClient httpclient = HttpClients.createDefault();
		// 创建httppost
		HttpPost httppost = new HttpPost(url);
		// httppost.addHeader("Content-Type",ContentType.APPLICATION_JSON.getMimeType());
		try {
			StringEntity stringEntity = new StringEntity(JsonUtil.mapToJsonString(map), "utf-8");
			stringEntity.setContentType(ContentType.APPLICATION_JSON.getMimeType());
			stringEntity.setContentEncoding("UTF-8");
			httppost.setEntity(stringEntity);
			System.out.println("executing request " + httppost.getURI());
			System.out.println("executing body " + JsonUtil.mapToJsonString(map));
			CloseableHttpResponse response = httpclient.execute(httppost);
			response.setHeader("Content-Type", "application/json");
			try {
				HttpEntity entity = response.getEntity();
				if (entity != null) {
					result = EntityUtils.toString(entity, "UTF-8");
					System.out.println("--------------------------------------");
					System.out.println("Response content: " + result);
					System.out.println("--------------------------------------");
				}
			} finally {
				response.close();
			}
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (UnsupportedEncodingException e1) {
			e1.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			// 关闭连接,释放资源
			try {
				httpclient.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return result;
	}
	
	/**
	 * 请求方式为json字符串的post请求
	 * @param str
	 * @param url
	 * @return
	 */
	public static String postWithString(String str, String url) {
		String result = null;
		// 创建默认的httpClient实例.
		CloseableHttpClient httpclient = HttpClients.createDefault();
		// 创建httppost
		HttpPost httppost = new HttpPost(url);
		// httppost.addHeader("Content-Type",ContentType.APPLICATION_JSON.getMimeType());
		try {
			StringEntity stringEntity = new StringEntity(str, "utf-8");
			stringEntity.setContentType(ContentType.APPLICATION_JSON.getMimeType());
			stringEntity.setContentEncoding("UTF-8");
			httppost.setEntity(stringEntity);
			System.out.println("executing request " + httppost.getURI());
			System.out.println("executing body " + str);
			CloseableHttpResponse response = httpclient.execute(httppost);
			response.setHeader("Content-Type", "application/json");
			try {
				HttpEntity entity = response.getEntity();
				if (entity != null) {
					result = EntityUtils.toString(entity, "UTF-8");
					System.out.println("--------------------------------------");
					System.out.println("Response content: " + result);
					System.out.println("--------------------------------------");
				}
			} finally {
				response.close();
			}
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (UnsupportedEncodingException e1) {
			e1.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			// 关闭连接,释放资源
			try {
				httpclient.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return result;
	}

	/**
	 * map 装换为 NameValuePair
	 * 
	 * @param map
	 * @return
	 */
	private static List<NameValuePair> parseMap(Map<String, Object> map) {
		List<NameValuePair> nameValuePairs = new ArrayList<>();
		for (String key : map.keySet()) {
			nameValuePairs.add(new BasicNameValuePair(key, map.get(key).toString()));
		}
		return nameValuePairs;
	}
	public static void main(String[] args) {
		Map<String, Object> map = new HashMap<String,Object>();
		map.put("status", "bind");
		//get(map, "http://169.254.108.212:8081/SocialBank/sys/user/bank/card");
		get(new HashMap<String,Object>(), "169.254.108.212:8080/nybank-socialbank-web/bank/user/month");
	}
}
